import sys
import httplib

def pode_repartir(url):
	"""Verifica se eh possivel repartir um download"""
	conn = httplib.HTTPConnection(sys.argv[1], 80)
	conn.putrequest("GET", "/")
	conn.putheader("Range", 0-0)
	conn.endheaders()
	resp = conn.getresponse()
	ret = resp.getheader('Accept-Ranges')
	conn.close()
	return ret != None

def baixar(url):
	print url
	if pode_repartir(url):
		print "Pode repartir!!!"
		repartir = True
	else:
		print "Nao da pra repartir..."
		repartir = False


	conn = httplib.HTTPConnection(url, 80)
	conn.putrequest("GET", "/")
	if repartir:
		conn.putheader("Range", "bytes=0-9")
	conn.endheaders()
	resp = conn.getresponse()
	if resp.status == 301:
		location = resp.getheader("Location")
		#if location.startswith("http://"):
		#	location = location.rpartition("http://")[2]
		ret = baixar(location)
	else:
		ret = resp.read()
	conn.close()
	
	return ret

print baixar(sys.argv[1])
	